<?php
namespace jy_51job;
defined('BY_JYA') or exit('error');
class kefu extends \plugin{
	public function addchatlog($postdata=array()){
		$chatmsg = trim($postdata['msg']);
		$type=$postdata['type'];
		if(empty($postdata['tcpid'])){
			$return['error'] = 1;
			$return['msg'] = '会话丢失！';
			exi($return);
		}
		if(empty($_SESSION['uid'])){
			$return['error'] = 1;
			$return['msg'] = '请先登录会员！';
			exi($return['msg'],'error');
		}
		if($type==4){
			$chatmsg=$postdata['image'];
		}
		if($type==7){
			$chatmsg=$postdata['file'];
		}
		if($type==8){
			$chatmsg=$postdata['goodsid'];
		}
		if(empty($chatmsg)){
			$return['error'] = 1;
			$return['msg'] = '请输入对话内容！';
			exi($return['msg'],'error');
		}
		if($type == 1){
			//预留
		}elseif($type == 3){
			//语言
		}elseif($type == 4){
			//图片
		}elseif($type == 7){
			//文件
				$fileurl=tomedia($postdata['msg']);
				$pathinfo=pathinfo($postdata['msg']);
				$fileicon=$pathinfo[extension];
				$filename=pdo_getcolumn('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$chatmsg),'title');
				$filename=$filename.'.'.$pathinfo[extension];
				if(strpos($fileicon,'xls')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'pd')){
					$fileicon='icon-pdf';
				}elseif(strpos($fileicon,'do')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'zip') || strpos($fileicon,'rar')){
					$fileicon='icon-zip';
				}else{
					$fileicon='icon-copy';
				}
				$filesize=filesize(ROOT_AT.'/'.$postdata['msg']);
				$filesize=price($filesize/1024,'int');
				$filesize .='kb';
				$fileinfo['filename']=$filename;
				$fileinfo['fileurl']=$fileurl;
				$fileinfo['filesize']=$filesize;
				$fileinfo['fileicon']=$fileicon;
		}elseif($type == 8){
			//商品
			$goods=pdo_get('jy_weishop_goods',array('id'=>$chatmsg,'uniacid'=>$_SESSION['uniacid']),array('id','title','price','image'));
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tcpid'=>$postdata['tcpid'],
			'msg'=>$chatmsg,
			'type'=>$type,
			'goods'=>ijson_encode($goods),
			'fileinfo'=>ijson_encode($fileinfo),
			'workerid'=>$postdata['workerid'],
			'is_worker'=>$postdata['is_worker'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aikefu_tcplog',$data);
		$logid = pdo_insertid();
		$res['log']=array(
			'id'=>$logid,
			'mid'=>$_SESSION['uid'],
			'type'=>$type,
			'goods'=>$goods,
			'fileinfo'=>$fileinfo,
			'tcpid'=>$postdata['tcpid'],
			'workerid'=>$postdata['workerid'],
			'msg'=>$chatmsg,
			'is_worker'=>$postdata['is_worker'],
		);
		return $res;
	}
	public function gettcpworkerid($groupid=0){
		if($groupid){
			$workerids=pdo_getall('jy_aikefu_worker',array('groupid'=>$groupid,'uniacid'=>$_SESSION['uniacid'],'status'=>1),'id');
		}else{
			$workerids=pdo_getall('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),'id');
		}
		if(!$workerids){
			return 0;
		}
		$num=count($workerids);
		$key=rand(0,$num);
		$workerid=$workerids[$key]['id'];
		return $workerid;
	}
}